#include "wap_label.h"
#include "wa_wm.h"
#include "wa_win.h"
#include "wa_label.h"
#include "wa_screen.h"
#include "wap.h"

static void wap_label_create(void);
static void wap_label_del(void);
static void wap_label_callback(wa_sign* sign);

wa_screen_obj wap_label_screen = 
{
	.id = WAP_WIN_LABEL_ID,
	.create = wap_label_create,
	.del = wap_label_del,
};

wa_obj* wap_label_win;
wa_obj* wap_label_label;

extern wa_font font_song_16_ascii;

uint8_t wap_label_color;
uint8_t wap_label_h_pos;
uint8_t wap_label_v_pos;

void wap_label_init(void)
{
	wap_label_color = 0;
	wap_label_h_pos = 0;
	wap_label_v_pos = 0;
}

static void wap_label_get_color(wa_gui_color* fore, wa_gui_color* back)
{
	if(wap_label_color == 0)
	{
		*fore = WA_COLOR_FOREGROUND;
		*back = WA_COLOR_BACKGROUND;
	}
	else
	{
		*fore = WA_COLOR_BACKGROUND;
		*back = WA_COLOR_FOREGROUND;
	}
}

static uint8_t wap_label_get_align_mode(void)
{
	uint8_t align_mode_h;
	uint8_t align_mode_v;
	if(wap_label_h_pos == 0)
	{
		align_mode_h = WA_LABEL_AL_LEFT;
	}
	else if(wap_label_h_pos == 1)
	{
		align_mode_h = WA_LABEL_AL_HCENTER;
	}
	else if(wap_label_h_pos == 2)
	{
		align_mode_h = WA_LABEL_AL_RIGHT;
	}
	
	if(wap_label_v_pos == 0)
	{
		align_mode_v = WA_LABEL_AL_TOP;
	}
	else if(wap_label_v_pos == 1)
	{
		align_mode_v = WA_LABEL_AL_VCENTER;
	}
	else if(wap_label_v_pos == 2)
	{
		align_mode_v = WA_LABEL_AL_BOTTOM;
	}
	
	return (align_mode_h | align_mode_v);
}

static void wap_label_create(void)
{
	uint8_t align_mode;
	wa_gui_color fore;
	wa_gui_color back;
	wap_label_win = wa_win_create(NULL);
	wa_win_set_pos(wap_label_win, 0, 0);
	wa_win_set_size(wap_label_win, WA_LCD_W, WA_LCD_H);
	wa_win_set_edge_show(wap_label_win, wa_nok);
	wa_win_set_callback(wap_label_win, wap_label_callback);
	wap_label_screen.win = wap_label_win;
	
	wap_label_label = wa_label_create(NULL);
	wa_win_add_obj(wap_label_win, wap_label_label);
	wa_obj_set_pos(wap_label_label, 0, 0);
	wa_obj_set_size(wap_label_label, WA_LCD_W, WA_LCD_H);
	wa_label_set_font(wap_label_label, &font_song_16_ascii);
	wa_label_set_text(wap_label_label, "Hello WaGUI");
	wa_label_set_col_space(wap_label_label, 0);
	wa_label_set_row_space(wap_label_label, 0);
	
	wap_label_get_color(&fore, &back);
	wa_label_set_text_color(wap_label_label, fore);
	wa_label_set_back_color(wap_label_label, back);
	
	align_mode = wap_label_get_align_mode();
	wa_label_set_align_mode(wap_label_label, align_mode);
}

static void wap_label_del(void)
{
	wa_obj_del(wap_label_win);
}

static void _wap_label_paint(wa_obj* obj)
{
}

static void wap_label_callback(wa_sign* sign)
{
	int sign_id;
	wa_obj* obj;
	sign_id = sign->sign_id;
	obj = sign->obj_dst;

	switch (sign_id)
	{
		case WA_SIGNAL_PAINT:
		{
			_wap_label_paint(obj);
			break;
		}
		case WA_SIGNAL_NOTIFY_PARENT:
		{	
			break;
		}
		case WA_SIGNAL_USER_KEY_UP:
		{	
			uint8_t align_mode;
			if(wap_label_v_pos > 0)
			{
				wap_label_v_pos--;
			}
			align_mode = wap_label_get_align_mode();
			wa_label_set_align_mode(wap_label_label, align_mode);
			break;
		}
		case WA_SIGNAL_USER_KEY_DOWN:
		{	
			uint8_t align_mode;
			if(wap_label_v_pos < 2)
			{
				wap_label_v_pos++;
			}
			align_mode = wap_label_get_align_mode();
			wa_label_set_align_mode(wap_label_label, align_mode);
			break;
		}
		case WA_SIGNAL_USER_KEY_LEFT:
		{	
			uint8_t align_mode;
			if(wap_label_h_pos > 0)
			{
				wap_label_h_pos--;
			}
			align_mode = wap_label_get_align_mode();
			wa_label_set_align_mode(wap_label_label, align_mode);
			break;
		}
		case WA_SIGNAL_USER_KEY_RIGHT:
		{	
			
			uint8_t align_mode;
			if(wap_label_h_pos < 2)
			{
				wap_label_h_pos++;
			}
			align_mode = wap_label_get_align_mode();
			wa_label_set_align_mode(wap_label_label, align_mode);
			break;
		}
		case WA_SIGNAL_USER_KEY_ENTER:
		{	
			wa_gui_color fore;
			wa_gui_color back;
			if(wap_label_color == 0)
			{
				wap_label_color = 1;
			}
			else
			{
				wap_label_color = 0;
			}
			wap_label_get_color(&fore, &back);
			wa_label_set_text_color(wap_label_label, fore);
			wa_label_set_back_color(wap_label_label, back);
			break;
		}
		case WA_SIGNAL_USER_KEY_ESC:
		{	
			wa_screen_switch(WAP_WIN_MENU_ID, WA_SCREEN_SWITCH_TYPE_LEFT_TOGETHER, 500, wa_nok, wa_nok);
			break;
		}
		default:
		{
			break;
		}
	}
}

